﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{ 
    public class ShipsController : Controller
    {
        private MainModelContainer db = new MainModelContainer();

        static int? ship_id;

        //
        // GET: /Ships/

        public ViewResult Index(bool? edit_ship,bool? delete_ship,bool? edit_cabins,bool? delete_cabins,bool? show_cabins,bool? empty=false,bool? no_cabins=false)
        {
            if (empty == true)
            {
                ModelState.AddModelError("", "Сначала создайте хотя бы один курс");
            }
            if (no_cabins == true)
            {
                ModelState.AddModelError("", "Сначала создайте хотя бы один корабль");
            }
            
            if (edit_ship == true)
            {
                ViewBag.edit_ship = true;
            }
            if (delete_ship == true)
            {
                ViewBag.delete_ship = true;
            }
            if (edit_cabins == true)
            {
                ViewBag.edit_cabins = true;
            }
            if (delete_cabins == true)
            {
                ViewBag.delete_cabins = true;
            }
            if (show_cabins == true)
            {
                ViewBag.show_cabins = true;
            }

            return View(db.Ships.ToList());
        }

        //
        // GET: /Ships/Details/5

        public ViewResult Details(int id)
        {
            Ship ship = db.Ships.Single(s => s.Id == id);
            return View(ship);
        }

        //
        // GET: /Ships/Create

        public ActionResult Create()
        {
            List<string> courses = (from i in db.Courses select i.Title).ToList();
            if (courses.Count == 0)
            {

                return RedirectToAction("Index", new { empty = true });
            }
            ViewBag.courses_list = new SelectList(courses);
            return View();
        } 

        //
        // POST: /Ships/Create

        [HttpPost]
        public ActionResult Create(Ship ship,string courses_list)
        {
            if (ModelState.IsValid)
            {
                Course c = (from i in db.Courses where i.Title == courses_list select i).FirstOrDefault();
                ship.Course = c;
                db.Ships.AddObject(ship);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(ship);
        }
        
        //
        // GET: /Ships/Edit/5
 
        public ActionResult Edit(int id)
        {
            Ship ship = db.Ships.Single(s => s.Id == id);
            return View(ship);
        }

        //
        // POST: /Ships/Edit/5

        [HttpPost]
        public ActionResult Edit(Ship ship)
        {
            if (ModelState.IsValid)
            {
                db.Ships.Attach(ship);
                db.ObjectStateManager.ChangeObjectState(ship, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ship);
        }

        //
        // GET: /Ships/Delete/5
 
        public ActionResult Delete(int id)
        {
            Ship ship = db.Ships.Single(s => s.Id == id);
            return View(ship);
        }

        //
        // POST: /Ships/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Ship ship = db.Ships.Single(s => s.Id == id);
            List<Cabins> cabins = (from i in ship.Cabins select i).ToList();
            for (int i = 0; i < cabins.Count; i++)
            {
                db.Cabins.DeleteObject(cabins.ElementAt(i));
            }
            db.CostShips.DeleteObject(ship.Cost);
            db.Ships.DeleteObject(ship);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult AddCabins(int? id)
        {
            if (id == null)
            {
                List<string> ships = (from i in db.Ships select i.Name).ToList();
                if (ships.Count == 0)
                {

                    return RedirectToAction("Index", new { no_cabins = true });
                }
                ViewBag.ships = new SelectList(ships);
            }
            else
            {
                ship_id = id;
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddCabins(Cabins cabin,string ships="")
        {
            Ship ship;
            if (ships == "")
            {
                ship = (from i in db.Ships where i.Id == ship_id select i).FirstOrDefault();
            }
            else
            {
                ship = (from i in db.Ships where i.Name == ships select i).FirstOrDefault();
            }
            if (ship != null)
            {
                ship.Cabins.Add(cabin);
                db.SaveChanges();
            }
            return RedirectToAction("Index");

        }
    }
}